<?php defined('BASEPATH') or die("No access");
    class Partner_model extends MY_Model
    {
        public function __construct()
        {
            parent::__construct();
        }
        //get all partner
        public function getAll($limit,$offset,$status)
        {
            $this->db->order_by('partner_order','asc');
            $this->db->limit($limit,$offset);
            if($status==0)
            {
                $this->db->where('partner_publish',0);
            }
            else
            {
                if($status>0)
                {
                   $this->db->where('partner_publish',1); 
                }
            }
            $query=$this->db->get('partner');
            return $query->result(); 
        }
        //get partner by id 
        public function getpartnerById($id)
        {
            $this->db->where('partner.partner_id',$id);
            $query=$this->db->get('partner');
            return $query->row();
        }
        
        public function getpartnerByAlias($alias)
        {
            $this->db->where('partner.partner_alias',$alias);
            $this->db->from('partner');
            $this->db->join('category_partner','category_partner.category_partner_id=partner.category_partner');
            $query=$this->db->get();
            return $query->row();
        }
        // insert or update 
        public function save($id,$data=array())
        {
            if($id==0)
            {
                //insert new record
                $this->db->insert('partner',$data);
            }
            else
            {
                $this->db->where('partner_id',$id);
                $this->db->update('partner',$data);
            }
            return $this->db->affected_rows();
        }
        
        //save order
        public function saveorder($id=array(),$order=array())
        {
            for($i=0;$i<count($id);$i++)
            {
                $this->db->where('partner_id',$id[$i]);
                $this->db->update('partner',array('partner_order'=>$order[$i]));
            }
        }
        //delete array partners
        public function delete($id=array())
        {
            $this->db->where_in('partner_id',$id);
            $this->db->delete('partner');
            return $this->db->affected_rows();
        }
        //publish or unpublish
        public function publish($status,$id=array())
        {
            $this->db->where_in('partner_id',$id);
            if($status==0)
            {
                $this->db->update('partner',array('partner_publish'=>0));
            }
            else
            {
                $this->db->update('partner',array('partner_publish'=>1));
            }
            return $this->db->affected_rows();
        }
    }
?>

